/**
 * @作者 Luke
 * @微信公号 欧诺的小书屋
 * @开源项目 $ http://7yue.pro
 * @免费专栏 $ http://course.7yue.pro
 * @我的课程 $ http://imooc.com/t/4294850
 * @创建时间 2020-6-21 22:26
 */
package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.dto.course.CreateOrUpdateChapterDTO;
import io.github.talelin.latticy.dto.course.CreateOrUpdateChapterItemDTO;
import io.github.talelin.latticy.mapper.ChapterItemMapper;
import io.github.talelin.latticy.mapper.ChapterMapper;
import io.github.talelin.latticy.model.ChapterDO;
import io.github.talelin.latticy.model.ChapterDetailDO;
import io.github.talelin.latticy.model.ChapterItemDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ChapterService extends ServiceImpl<ChapterMapper, ChapterDO> {

    @Autowired
    private ChapterItemMapper chapterItemMapper;

    /**
     * 根据courseId 和 teacherId 获取课程章节
     * @param courseId 课程id
     * @return List<ChapterDetailDO>
     */
//    public List<ChapterDetailDO> getChapters(Long courseId){
//        List<ChapterDetailDO> chapters = this.baseMapper.getChaptersByCourseId(courseId);
//        if (chapters == null || chapters.size() == 0) {
//            return new ArrayList<>();
//        }
//        return chapters;
//    }

    /**
     * 根据课程id 获取所有课程大章 小章
     * @param courseId 课程id
     * @return  ChapterDetailDO
     */
    public List<ChapterDetailDO> findChapters(Long courseId){
        List<ChapterDetailDO> chapterDetailDOS = new ArrayList<>();
        List<ChapterDO> chapters = this.baseMapper.findChaptersByCourseId(courseId);
        for (ChapterDO c : chapters) {
            ChapterDetailDO detail = new ChapterDetailDO();
            BeanUtils.copyProperties(c, detail);
            List<ChapterItemDO> chapterItems = this.getChapterItemsByCouseIdAndChapterId(courseId, detail.getId());
            detail.setChapterItemList(chapterItems);
            chapterDetailDOS.add(detail);
        }
        return chapterDetailDOS;
    }

    /**
     * 新增章节
     * @param validator CreateOrUpdateChapterDTO
     * @return boolean
     */
    public boolean createChapter(CreateOrUpdateChapterDTO validator){
        // 省略判断title是否相同
        int chapterNum = this.baseMapper.getCountByCourseId(validator.getCourseId());
        ChapterDO chapter = new ChapterDO();
        chapter.setTitle(validator.getTitle());
        chapter.setSubTitle(validator.getSubTitle());
        chapter.setCourseId(validator.getCourseId());
        chapter.setSort(chapterNum + 1L);
        chapter.setOnline(true);
        int i = this.baseMapper.insert(chapter);
        return i > 0;
    }

    /**
     * 修改章节信息
     * @param chapter 当前章节信息
     * @param validator CreateOrUpdateChapterDTO
     * @return boolean
     */
    public boolean updateChapter(ChapterDO chapter, CreateOrUpdateChapterDTO validator){
        chapter.setTitle(validator.getTitle());
        chapter.setSubTitle(validator.getSubTitle());
        chapter.setCourseId(validator.getCourseId());
        chapter.setSort(validator.getSort());
        return this.baseMapper.updateById(chapter) > 0;
    }

    /**
     * 新增小节
     * @param validator CreateOrUpdateChapterItemDTO
     * @return boolean
     */
    public boolean createChapterItem(CreateOrUpdateChapterItemDTO validator){
        // 省略判断title是否相同
        ChapterItemDO chapterItem = new ChapterItemDO();
        chapterItem.setOnline(true);
        BeanUtils.copyProperties(validator, chapterItem);
        int insert = this.chapterItemMapper.insert(chapterItem);
        return insert > 0;
    }

    /**
     *  修改小节信息
     * @param id id
     * @param validator CreateOrUpdateChapterItemDTO
     * @return boolean
     */
    public boolean updateChapterItem(Long id, CreateOrUpdateChapterItemDTO validator){
        ChapterItemDO chapterItem = this.chapterItemMapper.getById(id);
        if (chapterItem == null) {
            throw new NotFoundException("chapterItem not found", 10022);
        }
        chapterItem.setTitle(validator.getTitle());
        chapterItem.setChapterId(validator.getChapterId());
        chapterItem.setCourseId(validator.getCourseId());
        return this.chapterItemMapper.updateById(chapterItem) > 0;
    }

    /**
     * 根据courseId 和 chapterId 获取课程大章和小章
     * @param courseId 课程id
     * @param chapterId chapterId
     * @return ChapterItemDO
     */
    private List<ChapterItemDO> getChapterItemsByCouseIdAndChapterId(Long courseId,
                                                                     Long chapterId){
        return this.chapterItemMapper.getByCourseIdAndChapterId(courseId, chapterId);
    }
}
